<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
<!--    <el-form-item label="借阅证编号" prop="certnum">-->
<!--      <el-input v-model="dataForm.certnum" placeholder="借阅证编号"></el-input>-->
<!--    </el-form-item>-->
    <el-form-item label="姓名" prop="certname">
      <el-input v-model="dataForm.certname" placeholder="姓名"></el-input>
    </el-form-item>
      <el-form-item label="性别" prop="certsex">
        <el-select v-model="dataForm.certsex" placeholder="请选择">
          <el-option label="男" value="男"></el-option>
          <el-option label="女" value="女"></el-option>
        </el-select>
      </el-form-item>
    <el-form-item label="身份证号" prop="certidnum">
      <el-input v-model="dataForm.certidnum" placeholder="身份证号"></el-input>
    </el-form-item>
    <el-form-item label="电话号码" prop="certphone">
      <el-input v-model="dataForm.certphone" placeholder="电话号码"></el-input>
    </el-form-item>
    <el-form-item label="地址" prop="certaddress">
      <el-input v-model="dataForm.certaddress" placeholder="地址"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="certdescribe">
      <el-input v-model="dataForm.certdescribe" placeholder="描述"></el-input>
    </el-form-item>
      <el-form-item label="状态" prop="certstatus">
        <el-select v-model="dataForm.certstatus" placeholder="请选择">
          <el-option label="启用" value="启用"></el-option>
          <el-option label="禁用" value="禁用"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          certnum: '',
          certname: '',
          certsex: '',
          certidnum: '',
          certphone: '',
          certaddress: '',
          certdescribe: '',
          certstatus: ''
        },
        dataRule: {
          // certnum: [
          //   { required: true, message: '借阅证编号不能为空', trigger: 'blur' }
          // ],
          certname: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          certsex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          certidnum: [
            { required: true, message: '身份证号不能为空', trigger: 'blur' }
          ],
          certphone: [
            { required: true, message: '电话号码不能为空', trigger: 'blur' }
          ],
          certaddress: [
            { required: true, message: '地址不能为空', trigger: 'blur' }
          ],
          certdescribe: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ],
          certstatus: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/bs_borrcertificate/bsborrcertificate/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                // this.dataForm.certnum = data.bsBorrcertificate.certnum
                this.dataForm.certname = data.bsBorrcertificate.certname
                this.dataForm.certsex = data.bsBorrcertificate.certsex
                this.dataForm.certidnum = data.bsBorrcertificate.certidnum
                this.dataForm.certphone = data.bsBorrcertificate.certphone
                this.dataForm.certaddress = data.bsBorrcertificate.certaddress
                this.dataForm.certdescribe = data.bsBorrcertificate.certdescribe
                this.dataForm.certstatus = data.bsBorrcertificate.certstatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bs_borrcertificate/bsborrcertificate/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                // 'certnum': this.dataForm.certnum,
                'certname': this.dataForm.certname,
                'certsex': this.dataForm.certsex,
                'certidnum': this.dataForm.certidnum,
                'certphone': this.dataForm.certphone,
                'certaddress': this.dataForm.certaddress,
                'certdescribe': this.dataForm.certdescribe,
                'certstatus': this.dataForm.certstatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
